package com.suoluo.zenadmin.controllers.admin.country;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;

import com.suoluo.zenadmin.common.CommonResp;
import com.suoluo.zenadmin.common.JSONPageData;
import com.suoluo.zenadmin.common.PageUtil;
import com.suoluo.zenadmin.controllers.JSONInterceptor.JSON;
import com.suoluo.zenadmin.service.country.CountryService;
import com.suoluo.zenadmin.vo.PageVO;
import com.suoluo.zenadmin.vo.country.ContinentVO;
import com.suoluo.zenadmin.vo.country.CountryVO;

@Path("/")
public class CountryController {

	@Resource(name = "countryService")
	private CountryService countryService;

	@Get("vlist")
	public String toList() {
		return "listCountry";
	}

	@SuppressWarnings("rawtypes")
	@JSON
	@Post("list")
	public Object listCountry(PageVO param) {
		int page = param.getPage();
		int rows = param.getRows();
		int startPage = PageUtil.calcStartRow(page, rows);
		List<Map> list = countryService.findListCountry(startPage, rows);
		JSONPageData data = new JSONPageData();
		data.setRows(list);
		int total = countryService.findCountryCount();
		data.setTotal(total);
		return data;
	}

	@Get("vsave")
	public String toAdd() {
		return "addCountry";
	}

	@JSON
	@Post("save")
	public Object save(CountryVO countryVO) {
		countryService.saveCountry(countryVO);
		return CommonResp.SUCCESS;
	}

	@JSON
	@Post("continent/list")
	public Object listContinent() {
		List<ContinentVO> list = countryService.findListContinent();
		return list;
	}

	@Get("vupdate")
	public String toUpdate(Invocation invo) {
		int countryId = Integer.parseInt(invo.getParameter("countryId"));
		CountryVO country = countryService.findCountryById(countryId);
		invo.addModel("country", country);
		return "updateCountry";
	}

	@JSON
	@Post("update")
	public Object update(CountryVO countryVO) {
			countryService.update(countryVO);
			return CommonResp.SUCCESS;
	}

	@JSON
	@Post("delete")
	public Object delete(CountryVO country) {
		countryService.delete(country);
		return CommonResp.SUCCESS;
	}
}
